﻿using Group3.MediaTool.Provider;
using Group3.MediaTool.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using Group3.MediaTool.Data.Generator;
using Group3.MediaTool.Data.IO;
using System.IO;
using System.Reflection;
using System.Net;
using Group3.MediaTool.Generator;





namespace Group3.MediaTool.ConsoleSample
{
    class Program
    {

        static void Main(string[] args)
        {
      
            //testStartProgram();
            testStartProgramWeb(); 


            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);

        }

        private static void testStartProgramWeb()
        {

            string baseAddress = "http://localhost:9000/";

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseAddress + "api/displayobject/?classType=E_BOOK2");

             // Set some reasonable limits on resources used by this request
             request.MaximumAutomaticRedirections = 4;
             request.MaximumResponseHeadersLength = 4;
             // Set credentials to use for this request.
             request.Credentials = CredentialCache.DefaultCredentials;
             HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        }

        public static void testStartProgram()
        {

            String path = Path.Combine(StaticConfig.pathDataDefinition, "testBookExport.xml");

            LoadPrepare lp = new LoadPrepare();
            lp.updateImportfile(path, "Book");


            ClassBuilder cb = new ClassBuilder();


            StaticConfig.pathDataDefinitionFile = Path.Combine(StaticConfig.pathDataDefinition, "DataDefinitionWithData.xml");

            //Provider def
            SQLDatabaseProvider sqlConn = new SQLDatabaseProvider();
            sqlConn.createDatabase();
            sqlConn.dropAllTabels();

            //Clear
            sqlConn.dropAllTabels();

            DataDefinitionClass ddBig = new DataDefinitionClass();
            ddBig.createObjects();

            DataLoader dl = new DataLoader();
            List<Type> types = dl.loadAll(ddBig.Objects);

            String sqlCommand = "select count(ID) from BOOK";
            int i;
            sqlConn.runCommand(sqlCommand, "", out i);

            Type K = sqlConn.GetType();

            List<IObjectRelationModel> allData = new List<IObjectRelationModel>();
            foreach (Type T in types)
            {
                MethodInfo method2 = K.GetMethod("GetDataFromStore", new Type[] { });
                MethodInfo genericMethod2 = method2.MakeGenericMethod(T);

                var testt = genericMethod2.Invoke(sqlConn, null);
                if(testt != null && ((IEnumerable<IObjectRelationModel>)testt).Count() > 0){
                    allData.AddRange((IEnumerable<IObjectRelationModel>)testt);
                }
                
            }



        }

    }
}
